#pragma once
#include <iostream>
#include <unistd.h>
#include <string>
#include <time.h>
#include <cstdarg>
using namespace std;

enum
{
    Debug = 0,
    Information,
    Warning,
    Error,
    Fatal,
    Unknown
};

string leveltostring(int level)
{
    switch (level)
    {
    case 0:
        return string("Debug");
    case 1:
        return string("Information");
    case 2:
        return string("Warning");
    case 3:
        return string("Error");
    case 4:
        return string("Fatal");
    default:
        return string("Unknown");
    }
    
}
string timetostring()
{
    time_t t=time(nullptr);
struct tm* time=localtime(&t);
string str;
str+=to_string(time->tm_hour);
str+=':';
str+=to_string(time->tm_min);
str+=':';
str+=to_string(time->tm_sec);
str+=':';
return str;
}

// 日志格式：日志等级 时间 pid 消息体
void LogMessage(int level, char *format...)
{
    char leftmeg[1024];
    char rightmeg[1024];
    string levelstr = leveltostring(level);
    string timestr=timetostring();
    snprintf(leftmeg,sizeof(leftmeg),"等级：%s,时间：%s,pid:%d,消息：",levelstr.c_str(),timestr.c_str(),getpid());

    va_list p;//char*
    va_start(p,format);//p指向可变参数部分的起始地址
    vsnprintf(rightmeg,sizeof(rightmeg),format,p);
    va_end(p);

    cout<<leftmeg<<rightmeg<<endl;


    //写入文件
    /*FILE* fp=fopen("./log.txt","a");
    fprintf(fp,"%s%s\n",leftmeg,rightmeg);
    fflush(fp);
    fclose(fp);*/
}

